package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorAssistantSettingsDTO;
import com.pshcp.domain.model.DoctorAssistantSettings;
import com.pshcp.mapper.DoctorAssistantSettingsMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医院助手设置表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorAssistantSettingsService {

    @Autowired
    private DoctorAssistantSettingsMapper doctorAssistantSettingsMapper;

    public DoctorAssistantSettings getDoctorAssistantSettingsByDoctorId(Long doctorId) {
        Example example = new Example(DoctorAssistantSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorAssistantSettings> list = doctorAssistantSettingsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<DoctorAssistantSettings> list(DoctorAssistantSettingsDTO doctorAssistantSettingsDTO) {
        if (doctorAssistantSettingsDTO.getPageNum() != null && doctorAssistantSettingsDTO.getPageSize() != null) {
            PageHelper.startPage(doctorAssistantSettingsDTO.getPageNum(), doctorAssistantSettingsDTO.getPageSize());
        }
        Example example = new Example(DoctorAssistantSettings.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorAssistantSettingsDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorAssistantSettingsDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorAssistantSettings> list = doctorAssistantSettingsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorAssistantSettings> select(DoctorAssistantSettings doctorAssistantSettings) {
        return doctorAssistantSettingsMapper.select(doctorAssistantSettings);
    }

    public int insert(DoctorAssistantSettings doctorAssistantSettings) {
        doctorAssistantSettings.setId(SystemUtil.getId());
        doctorAssistantSettings.setStatus(CommonStatusEnum.INIT.getCode());
        doctorAssistantSettings.setCreatedTime(new Date());
        return doctorAssistantSettingsMapper.insert(doctorAssistantSettings);
    }

    public int update(DoctorAssistantSettings doctorAssistantSettings) {
        doctorAssistantSettings.setUpdateTime(new Date());
        return doctorAssistantSettingsMapper.updateByPrimaryKeySelective(doctorAssistantSettings);
    }

    public DoctorAssistantSettings findById(Long id) {
        return doctorAssistantSettingsMapper.selectByPrimaryKey(id);
    }

    public List<DoctorAssistantSettings> findByIds(List<Long> ids) {
        Example example = new Example(DoctorAssistantSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorAssistantSettingsMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorAssistantSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorAssistantSettings doctorAssistantSettings = new DoctorAssistantSettings();
        doctorAssistantSettings.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorAssistantSettingsMapper.updateByExampleSelective(doctorAssistantSettings, example);
    }

}
